package com.pignest.hotel.ui.member;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

/**
 * 修改会员(不含升级)
 */
public class EditMemberWindow extends Window implements EventListener {
	private static final long serialVersionUID = -8963457712668520072L;
	
	private MemberService service;
	private Member member;
	private boolean success;
	
	private Textbox nameField;		//会员姓名
	private Listbox sexListbox;		//性别
	private Textbox mobileField;	//手机
	private Listbox certificateTypeListbox;//证件类型
	private Textbox certificateNoField;	//证件号
	private Textbox mailField;		//邮件
	private Textbox streetField;	//街道;
	private Datebox birthdayField;
	
	private Listbox memberLevelListbox;//会员级别如金卡、银卡、普通
	private Label canSelfLivingLabel;
	private Label canSelfQuitLabel;
	private Label balanceLabel;
	private Label integralLabel;
	private Label overdrawLabel;
	private Label creditLabel;
	private Label timeLabel;
	
	private Textbox cardNoField;
	
//	private Button submitbtn;

	public EditMemberWindow() {
		service = Services.getMemberService();
		addEventListener("onCreate", this);
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		if ("onCreate".equals(event.getName())) {
			nameField=(Textbox)getFellow("nameField");
			sexListbox=(Listbox)getFellow("sexListbox");
			mobileField=(Textbox)getFellow("mobileField");
			certificateTypeListbox=(Listbox)getFellow("certificateTypeListbox");
			certificateNoField=(Textbox)getFellow("certificateNoField");
			mailField=(Textbox)getFellow("mailField");
			streetField=(Textbox)getFellow("streetField");
			birthdayField=(Datebox)getFellow("birthdayField");
			
			memberLevelListbox = (Listbox)getFellow("memberLevelListbox");
			canSelfLivingLabel = (Label)getFellow("canSelfLivingLabel");
			canSelfQuitLabel = (Label)getFellow("canSelfQuitLabel");
			balanceLabel = (Label)getFellow("balanceLabel");
			integralLabel = (Label)getFellow("integralLabel");
			overdrawLabel = (Label)getFellow("overdrawLabel");
			creditLabel = (Label)getFellow("creditLabel");
			timeLabel = (Label)getFellow("timeLabel");
			
			cardNoField=(Textbox)getFellow("cardNoField");
			
			changeLevelTip();
			memberLevelListbox.setDisabled(true);
			init();
		}
	}
	
	private void init() {
		Member member = this.member;
		nameField.setValue(member.getName());
		UIUtils.setSelectedItem(sexListbox, member.getSex());
//		sexListbox=(Listbox)getFellow("sexListbox");
		mobileField.setValue(member.getMobile());
		UIUtils.setSelectedItem(certificateTypeListbox, member.getCertificatType());
		certificateNoField.setValue(member.getCertificateNo());
		mailField.setValue(member.getMail());
		streetField.setValue(member.getStreet());
		birthdayField.setValue(member.getBirthday());
		
		cardNoField.setValue(member.getCardNo());
		
		MemberLevel level = member.getMemberLevel();
		UIUtils.setSelectedItem(memberLevelListbox, level.getId());
		canSelfLivingLabel.setValue(level.getSelfhelplive());
		canSelfQuitLabel.setValue(level.getSelfhelpout());
		balanceLabel.setValue(Utils.toPrice(member.getBalance()));
		integralLabel.setValue(String.valueOf(member.getIntegral()));
		overdrawLabel.setValue(Utils.toPrice(level.getOverdraft()));
		creditLabel.setValue(String.valueOf(member.getCredit()));
		timeLabel.setValue(DateUtil.toString(member.getDealTime(), DateUtil.DEFAULT_DATETIME_FORMAT));
	}
	
	public String save() {
//		if (!Hotel.onDuty()) {
//			return "你当前不在值班状态, 无法新增会员.";
//		}
		String name = nameField.getValue();
		if (StringUtils.isEmpty(name) || name.length() > 30) {
			return "用户名必填, 且在30字符以内";
		}
		String gender = UIUtils.getSelectedValue(sexListbox);
		String phone = mobileField.getValue();
		String ctype = UIUtils.getSelectedValue(certificateTypeListbox);
		String cid = certificateNoField.getValue();		//XXX 身份证验证		CheckUtil.validatePersonalId(value)
		if (StringUtils.isEmpty(cid)) {
			return "证件号码必填";
		}
		String email = mailField.getValue();
		String address = streetField.getValue();
		Date birthday = birthdayField.getValue();
		
		final String cardNumber = StringUtils.defaultIfEmpty(cardNoField.getValue(), null);
//		if (StringUtils.isEmpty(cardNumber))
//			return "请一张空卡做为会员卡";
		final Card card = cardNumber == null ? null : Services.getCardService().getCardByCardNumber(cardNumber);
		if (!Utils.equals(cardNumber, member.getCardNo())) {
			if (cardNumber != null) {
				if (card == null)
					return "此卡片未在系统中录入";
				if (!card.isEnable())
					return "此卡片已经被禁用";
				if (!card.isUsable())
					return "此卡片已经被使用, 或者未设置卡号";
			}
		}
		
		final Member m = this.member;
		m.setName(name);
		m.setSex(gender);
		m.setMobile(phone);
		m.setCertificatType(ctype);
		m.setCertificateNo(cid);
		m.setMail(email);
		m.setStreet(address);
		m.setBirthday(birthday);
		
		service.executeInTransaction(new Runnable() {
			@Override
			public void run() {
				if (!Utils.equals(cardNumber, m.getCardNo())) {
					if (StringUtils.isNotEmpty(m.getCardNo()))
						Services.getCardService().resetCard(m.getCardNo());
					if (card != null)
						Services.getCardService().grantCard(card, m);
				}
//				m.setCardNo(cardNumber);
				service.update(m);
				L.update(m);
			}
		});
		
		return null;
	}

	public void changeLevelTip() {
		String id = UIUtils.getSelectedValue(memberLevelListbox);
		MemberLevel level = service.getMemberLevel(id);

		canSelfLivingLabel.setValue(level.getSelfhelplive());
		canSelfQuitLabel.setValue(level.getSelfhelpout());
		overdrawLabel.setValue(Utils.toPrice(level.getOverdraft()));
		
	}
	
	public void setMember(Member selMember) {
		this.member = selMember;
	}
	
	public void onReadCard(String serial) {
		if (serial == null) {
			return;
		}
		Card card = Services.getCardService().getCardBySerialNumber(serial);
		if (card == null) {
			UIUtils.alert("该卡片未在系统中登记, 无法使用");
			return;
		}
		if (!card.isUsable()) {
			if (StringUtils.isEmpty(card.getCardNumber())){
				UIUtils.alert("该卡片无法使用, 卡号未设置");
				return;
			}
			UIUtils.alert("该卡片无法使用, 可能已经在使用中(" + card.getHolderName2() + ")");
			return;
		}
		cardNoField.setValue(card.getCardNumber());
	}
	
	public boolean isSuccess() {
		return success;
	}

//	public String getoPratorId() {
//		return oPratorId;
//	}
//
//	public void setoPratorId(String oPratorId) {
//		this.oPratorId = oPratorId;
//	}


    
}
